package database.dao.Impl;

import database.dao.OrderRecordDao;
import entity.*;

import javax.xml.crypto.Data;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName OrderRecordDaoImpl
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/17 8:52
 */
public class OrderRecordDaoImpl extends BaseDao implements OrderRecordDao {
    @Override
    public void addOrderRecord(OrderRecord orderRecord) {
        String psql="insert into orderrecord(invoice_id,commodity_id,quantity)" +
                "values(?,?,?)";
        Object[] values={orderRecord.getInvoice_id(),orderRecord.getCommodity_id(),orderRecord.getQuantity()};
        super.executeUpdate(psql,values);
    }

    @Override
    public List<UserOrder> selectOrderRecord(String customer_id) {
        String psql="SELECT o.`orderRecord_id`,c.`name`,o.`quantity`,o.`status`,i.`invoice_date`\n" +
                "FROM orderrecord o , commodity c,invoice i\n" +
                "WHERE o.`invoice_id` IN (SELECT i.`invoice_id` FROM invoice i  WHERE i.`customer_id`=? ) \n" +
                "AND c.`commodity_id`=o.`commodity_id`\n" +
                "AND i.`invoice_id`=o.`invoice_id`;";
        Object[] value={ customer_id };
        ResultSet resultSet= super.executeQuerySQL(psql,value);

        List<UserOrder>orders=new ArrayList<>();
        if(resultSet!=null){
            try{
                while(resultSet.next()){
                    Timestamp timestamp=resultSet.getTimestamp("invoice_date");
                    Date date=new Date(timestamp.getTime());

                    UserOrder userOrder=new UserOrder(resultSet.getInt("orderRecord_id"),resultSet.getString("name"),
                            resultSet.getInt("quantity"),resultSet.getString("status"),date.toString());
                    orders.add(userOrder);
                }
            }catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return orders;
    }

    @Override
    public List<BusinessOrder>  business_selectOrderRecord(String business_id) {

        String psql="SELECT o.`orderRecord_id`,cu.name,cu.`address`,c.`name`,o.`quantity`,o.`status`,i.`invoice_date`\n" +
                "FROM orderrecord o , commodity c,invoice i,customer cu\n" +
                "WHERE o.commodity_id IN( SELECT commodity_id FROM commodity WHERE business_id= ? )\n" +
                "AND c.commodity_id= o.commodity_id\n" +
                "AND i.`invoice_id`=o.`invoice_id`\n" +
                "AND cu.customer_id=i.`customer_id`;";
        Object[] value={ business_id };
        ResultSet resultSet= super.executeQuerySQL(psql,value);

        List<BusinessOrder>orders=new ArrayList<>();
        if(resultSet!=null){
            try{
                while(resultSet.next()){
                    Timestamp timestamp=resultSet.getTimestamp("invoice_date");
                    Date date=new Date(timestamp.getTime());

                    BusinessOrder businessOrder=new BusinessOrder(resultSet.getInt("orderRecord_id"),resultSet.getString(4),
                            resultSet.getString(2),resultSet.getString("address"),resultSet.getInt("quantity"),resultSet.getString("status"),date.toString());
                    orders.add(businessOrder);
                }
            }catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return orders;
    }

    @Override
    public void updateOrderStatus(String order_id, String newStatus) {
        System.out.println("updateOrderStatus");
        String psql="update  OrderRecord set status = ? where orderRecord_id=?";
        Object[] values={newStatus,order_id};
        super.executeUpdate(psql,values);
    }
}
